import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/entity/motorcade_entity.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/utils/format_utils.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/widgets/custom_popup_menu_item.dart';
import 'package:nyh_motorcade_app/widgets/flexible_icon_text.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:nyh_motorcade_app/widgets/text_button_with_icon.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'motorcade_account_controller.dart';

class MotorcadeAccountPage extends StatelessWidget {
  final controller = Get.put(MotorcadeAccountController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(context: context, title: "车队账户"),
      body: GetBuilder<MotorcadeAccountController>(
        builder: (controller) {
          return Column(
            children: [
              buildHeaderWidget(),
              buildListWidget(),
            ],
          );
        },
      ),
    );
  }

  Widget buildHeaderWidget() {
    return Container(
      padding: EdgeInsets.only(left: 8.w, right: 16.w),
      height: 40.w,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          TextButtonWithIcon(
            onPressed: () {
              showMotorcadeMenu();
            },
            icon: Image.asset(controller.showMotorcadeMenu ? "images/ic_arrow_up.png" : "images/ic_arrow_down.png"),
            label: Text(
              controller.motorcadeName.isNotEmpty ? controller.motorcadeName : "全部车队",
              style: TextStyle(
                color: Colours.color_text_theme,
                fontSize: 14.sp,
              ),
            ),
            textIconPadding: 5.w,
            iconTextAlignment: IconTextAlignment.iconRightTextLeft,
          ),
          Text(
            "共${controller.motorcadeShowList.length}条",
            style: TextStyle(
              color: Colours.color_b7b7c0,
              fontSize: 14.sp,
            ),
          )
        ],
      ),
    );
  }

  Future<void> showMotorcadeMenu() async {
    if (controller.motorcadeFilterList.isEmpty) {
      return;
    }
    controller.showMotorcadeMenu = true;
    controller.update();
    showMenu(
      context: Get.context!,
      position: RelativeRect.fromLTRB(16.w, ScreenUtil().statusBarHeight + 56 + 40.w, ScreenUtil().screenWidth, 0.w),
      items: controller.motorcadeFilterList.asMap().keys.map((e) => CustomPopupMenuItem(e, controller.motorcadeFilterList[e].name ?? "")).toList(),
      elevation: 5.w,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10.w),
      ),
    ).then((value) {
      controller.setSelectMotorcade(value);
    });
  }

  Widget buildListWidget() {
    return Expanded(
      child: MultiStateWidget(
        child: SmartRefresher(
          controller: controller.refreshController,
          onRefresh: controller.onRefresh,
          child: ListView.builder(
            shrinkWrap: true,
            physics: BouncingScrollPhysics(),
            itemCount: controller.motorcadeShowList.length,
            itemBuilder: (context, index) {
              return buildItemWidget(index);
            },
          ),
        ),
        state: controller.multiState,
        onRetry: () {
          controller.onRetry();
        },
      ),
    );
  }

  Widget buildItemWidget(int index) {
    MotorcadeEntity entity = controller.motorcadeShowList[index];
    List<String> subCompanyList = entity.subCompanyList ?? [];
    return Container(
      margin: EdgeInsets.only(bottom: 12.w),
      color: Colours.color_white,
      child: Material(
        child: InkWell(
          onTap: () {
            controller.goToCashFlowPage(entity.id);
          },
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 16.w),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Expanded(
                          child: Text(
                            entity.name ?? "",
                            style: TextStyle(
                              color: Colours.color_text_theme,
                              fontSize: 16.sp,
                              fontWeight: FontWeight.bold,
                            ),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                        SizedBox(
                          width: 10.w,
                        ),
                        Visibility(
                          visible: entity.status == 0,
                          child: Container(
                            padding: EdgeInsets.only(left: 5.w, right: 5.w, bottom: 2.w),
                            decoration: BoxDecoration(
                              color: Colours.color_cfcfd5,
                              borderRadius: BorderRadius.circular(5.w),
                              border: Border.all(width: 1.w, color: Colours.color_cfcfd5),
                            ),
                            child: Text(
                              "已禁用",
                              style: TextStyle(
                                color: Colours.color_white,
                                fontSize: 12.sp,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: subCompanyList.isNotEmpty ? 7.w : 0,
                    ),
                    FlexibleIconText(
                      icon: Image.asset(
                        "images/ic_company.png",
                        height: 12.w,
                        width: 12.w,
                      ),
                      textList: subCompanyList,
                      iconTextPadding: 5.w,
                    ),
                    SizedBox(
                      height: 10.w,
                    ),
                    Row(
                      children: [
                        Text(
                          "余额(元) ${FormatUtils.formatMoney(entity.availableAmount)}",
                          style: TextStyle(color: Colours.color_6f7082, fontSize: 12.sp),
                        )
                      ],
                    ),
                  ],
                ),
              ),
              Divider(
                color: Colours.color_divider,
                height: 1.w,
                thickness: 1.w,
              ),
              Padding(
                padding: EdgeInsets.symmetric(vertical: 5.w, horizontal: 16.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: Text(
                        entity.code ?? "",
                        style: TextStyle(color: Colours.color_cfcfd5, fontSize: 14.sp),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    SizedBox(
                      width: 10.w,
                    ),
                    Visibility(
                      visible: controller.permissionTransferMotorcade,
                      child: TextButton(
                        onPressed: () {
                          controller.goToRapidTransferPage(entity.id, entity.name, entity.availableAmount);
                        },
                        child: Text(
                          "划拨",
                          style: TextStyle(
                            color: Colours.color_theme,
                            fontSize: 14.sp,
                          ),
                        ),
                        style: ButtonStyle(
                          tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                          minimumSize: MaterialStateProperty.all(Size(80.w, 30.w)),
                          padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                          side: MaterialStateProperty.all(BorderSide(width: 1.w, color: Colours.color_theme)),
                          shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.w))),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
